 //
//  MPCRotateLoadingView.m
//  Pods
//
//  Created by macRong on 2017/9/4.
//
//


#import "MPCRotateLoadingView.h"


@interface MPCRotateLoadingView ()
{
    UIImageView *imageViewLoading;
    
    BOOL        rotating;
    double      imageviewAngle;
}

@end


@implementation MPCRotateLoadingView


#pragma mark - ********************** View Lifecycle **********************

- (void)dealloc
{
    NSLog(@"%s ✅",__FUNCTION__);
    
    [imageViewLoading.layer removeAllAnimations];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self)
    {
        // 注册消息
        [self regNotification];
        
        // 初始化变量
        [self initVariable];
        
        // 创建相关子view
        [self initMainViews];
    }
    
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    
    if (self)
    {
        // 注册消息
        [self regNotification];
        
        // 初始化变量
        [self initVariable];
        
        // 创建相关子view
        [self initMainViews];
    }
    
    return self;
}

#pragma mark - ********************** init and config **********************

/**
 TODO: 初始化变量，例如：分页的page可以在该函数中赋初值
 */
- (void)initVariable
{
    
}

/**
 TODO: 创建相关子view
 */
- (void)initMainViews
{
 
    
}

/**
 TODO: 注册通知
 */
- (void)regNotification
{
    //[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(appWillActiveAction:) name:UIApplicationDidBecomeActiveNotification object:nil];

}

- (void)appendImage
{
    for (UIView * view in [self subviews])
    {
        [view removeFromSuperview];
    }
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.frame = self.bounds;
    imageView.image = self.rotateImage;
    
    [self addSubview:imageView];
    
    imageViewLoading = imageView;
}

- (void)startAnimating
{
    if (self.style==MPCRotateLoadingViewPlayerStyle && imageViewLoading && imageViewLoading.isAnimating)
    {
        return;
    }
    
    self.hidden = NO;
    rotating = YES;
    imageviewAngle = 0;
    
    if (self.style==MPCRotateLoadingViewPlayerStyle)
    {
        if (!imageViewLoading)
        {
            imageViewLoading = [[UIImageView alloc]init];
            imageViewLoading.frame = self.bounds;
            [self addSubview:imageViewLoading];
        }
        
        imageViewLoading.hidden = NO;
        imageViewLoading.animationImages = self.uniqueGIFImages;
        imageViewLoading.animationDuration = 2;
        [imageViewLoading startAnimating];
    }
    else
    {
        //方案1
        [self appendImage];
        CABasicAnimation* rotationAnimation;
        rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        rotationAnimation.removedOnCompletion = NO;
        rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
        rotationAnimation.duration = 1;
        rotationAnimation.cumulative = YES;
        rotationAnimation.repeatCount = NSIntegerMax;
        [imageViewLoading.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    }
    
    //方案2
    //    [self rotate];
}

- (void)rotate
{
    if (rotating==NO)
    {
        return;
    }
    
    CGAffineTransform endAngle = CGAffineTransformMakeRotation(imageviewAngle * (M_PI / 180.0f));
    
    [UIView animateWithDuration:0.01 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        imageViewLoading.transform = endAngle;
    } completion:^(BOOL finished) {
        imageviewAngle += 6;
        [self rotate];
    }];
}

- (void)stopAnimating
{
    self.hidden = YES;
    rotating = NO;
    
    if (self.style == MPCRotateLoadingViewPlayerStyle)
    {
        if (imageViewLoading)
        {
            [imageViewLoading stopAnimating];
            imageViewLoading = nil;
        }
    }
    else
    {
        //方案1
        [imageViewLoading.layer removeAnimationForKey:@"rotationAnimation"];
        [imageViewLoading removeFromSuperview];
        imageViewLoading = nil;
    }
}

- (BOOL)isAnimating
{
    if (self.style==MPCRotateLoadingViewPlayerStyle)
    {
        return imageViewLoading.isAnimating;
    }
    else
    {
        return rotating;
    }
}

#pragma mark - ******************************************* 对外方法 **********************************


#pragma mark - ******************************************* 基类方法(一般发生在重写函数) ****************


#pragma mark - ******************************************* Touch Event ***********************


#pragma mark - ******************************************* 私有方法 **********************************


#pragma mark - ******************************************* Net Connection Event ********************

- (void)appWillActiveAction:(NSNotification *)notifi
{
    if (imageViewLoading) //![self isAnimating])
    {
        [self startAnimating];
    }
}


#pragma mark - ******************************************* Delegate Event **************************

#pragma mark - 代理 demo

- (void)delegate_demo
{
    
}


#pragma mark - ******************************************* Notification Event **********************

#pragma mark - 通知 demo

- (void)notification_demo:(NSNotification *)aNotification
{
    
}


#pragma mark - ******************************************* 属性变量的 Set 和 Get 方法 *****************


@end
